/**
 * Project: ld.biz.common
 * 
 * File Created at 2012-9-26
 * $Id$
 * 
 * Copyright 1999-2100 Alibaba.com Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Alibaba Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Alibaba.com.
 */
package com.liangdev.common.util;

/**
 * TODO Comment of XmlFormat
 * @author chongan.wangca
 *
 */

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlFormatUtil {

    public static String format(String str) throws UnsupportedEncodingException, IOException,
            DocumentException {
        // System.out.println(" str :  " + str);

        SAXReader reader = new SAXReader();
        // System.out.println(reader);
        // 注释：创建一个串的字符输入流
        StringReader in = new StringReader(str);
        Document doc = reader.read(in);
        // System.out.println(doc.getRootElement());
        // 注释：创建输出格式
        OutputFormat formater = OutputFormat.createPrettyPrint();
        //formater=OutputFormat.createCompactFormat();
        // 注释：设置xml的输出编码
        formater.setEncoding("utf-8");
        // 注释：创建输出(目标)
        StringWriter out = new StringWriter();
        // 注释：创建输出流
        XMLWriter writer = new XMLWriter(out, formater);
        // 注释：输出格式化的串到目标中，执行后。格式化后的串保存在out中。
        writer.write(doc);

        writer.close();
        System.out.println(out.toString());
        // 注释：返回我们格式化后的结果
        return out.toString();
    }

    public static void main(String[] args) throws Exception {
        //        String head = "<?xml version=\"1.0\" encoding=\"GBK\"?>";
        //        String str = "<RequestData><HeadData><UserCode>sh1_admin</UserCode><UserName>sh1_admin</UserName><UserCompanyCode>3107</UserCompanyCode><UserCompanyName>上海分公司一部</UserCompanyName><RequestType>03</RequestType></HeadData><BodyData><ReportId>113100000033</ReportId><Insurant>a5rfg87</Insurant><NumberPlate>沪E78612</NumberPlate><EngineModel></EngineModel><CarVin></CarVin><AccidentDate>2011-02-25 15:07:00</AccidentDate><ReportDate>2011-02-25 15:07:00</ReportDate><Province>310000</Province><City>310100</City><District></District><AccidentPlace>1</AccidentPlace><AccidentLongitude></AccidentLongitude><AccidentLatitude></AccidentLatitude><SurveyLongitude></SurveyLongitude><SurveyLatitude></SurveyLatitude><SceneReportFlag></SceneReportFlag><Reporter></Reporter><ReporterTel></ReporterTel><SurveyPlace></SurveyPlace><OperatorId>3525</OperatorId><OperatorName>sh_admin</OperatorName><ReportDealId>30000800</ReportDealId><ReportDealName>江苏分公司</ReportDealName><CompanyName></CompanyName><CustomerTypeCode></CustomerTypeCode><ForcePolicyId>a5rfg87a5rfg87a5rfg87</ForcePolicyId><BizPolicyId></BizPolicyId><Index>0</Index><FieldName>5</FieldName></BodyData></RequestData>";
        // System.out.println(str);
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>                                                                              "
                + "<!DOCTYPE sqlMap PUBLIC \"-//ibatis.apache.org//DTD SQL Map 2.0//EN\" \"http://ibatis.apache.org/dtd/sql-map-2.dtd\" >   "
                + "                                                                                                                         "
                + "<sqlMap namespace=\"TemplateInfo\" >                                                                                     "
                + "                                                                                                                         "
                + "  <typeAlias alias=\"templateInfoDO\"    type=\"com.liangdev.biz.dataobject.autocode.TemplateInfoDO\" />                 "
                + "  <typeAlias alias=\"templateInfoQuery\" type=\"com.liangdev.biz.query.autocode.TemplateInfoQuery\" />                   "
                + "                                                                                                                         "
                + "  <resultMap id=\"templateInfoDOResultMap\" class=\"templateInfoDO\" >                                                   "
                + "    <result property=\"id\" column=\"id\" />                                                                             "
                + "    <result property=\"templateName\" column=\"template_name\" />                                                        "
                + "    <result property=\"title\" column=\"title\" />                                                                       "
                + "    <result property=\"remark\" column=\"remark\" />                                                                     "
                + "    <result property=\"fileName\" column=\"file_name\" />                                                                "
                + "    <result property=\"groupName\" column=\"group_name\" />                                                              "
                + "    <result property=\"rank\" column=\"rank\" />                                                                         "
                + "  </resultMap>                                                                                                           "
                + "                                                                                                                         "
                + "  <sql id=\"templateInfo_columns\" >                                                                                     "
                + "    id, template_name, title, remark, file_name, group_name, rank                                                        "
                + "  </sql>                                                                                                                 "
                + "                                                                                                                         "
                + "  <sql id=\"templateInfo_queryParams\" >                                                                                 "
                + "    <dynamic prepend=\"WHERE\" >                                                                                         "
                + "      <isNotNull property=\"id\" prepend=\"AND\" >                                                                       "
                + "        <![CDATA[                                                                                                        "
                + "        id = #id#                                                                                                        "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"templateName\" prepend=\"AND\" >                                                             "
                + "        <![CDATA[                                                                                                        "
                + "        template_name = #templateName#                                                                                   "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"title\" prepend=\"AND\" >                                                                    "
                + "        <![CDATA[                                                                                                        "
                + "        title = #title#                                                                                                  "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"remark\" prepend=\"AND\" >                                                                   "
                + "        <![CDATA[                                                                                                        "
                + "        remark = #remark#                                                                                                "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"fileName\" prepend=\"AND\" >                                                                 "
                + "        <![CDATA[                                                                                                        "
                + "        file_name = #fileName#                                                                                           "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"groupName\" prepend=\"AND\" >                                                                "
                + "        <![CDATA[                                                                                                        "
                + "        group_name = #groupName#                                                                                         "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"rank\" prepend=\"AND\" >                                                                     "
                + "        <![CDATA[                                                                                                        "
                + "        rank = #rank#                                                                                                    "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "    </dynamic>                                                                                                           "
                + "  </sql>                                                                                                                 "
                + "                                                                                                                         "
                + "                                                                                                                         "
                + "                                                                                                                         "
                + "  <insert id=\"templateInfo.add\" parameterClass=\"templateInfoDO\" >                                                    "
                + "    INSERT INTO ld_template_info(template_name, title, remark, file_name, group_name, rank)                              "
                + "                VALUES (#templateName#, #title#, #remark#, #fileName#, #groupName#, #rank#)                              "
                + "    <selectKey keyProperty=\"id\" resultClass=\"java.lang.Long\" >                                                       "
                + "      SELECT LAST_INSERT_ID() AS value                                                                                   "
                + "    </selectKey>                                                                                                         "
                + "  </insert>                                                                                                              "
                + "                                                                                                                         "
                + "                                                                                                                         "
                + "                                                                                                                         "
                + "  <update id=\"templateInfo.update\" parameterClass=\"templateInfoDO\" >                                                 "
                + "    UPDATE ld_template_info                                                                                              "
                + "    <dynamic prepend=\"SET\" >                                                                                           "
                + "      <isNotNull property=\"templateName\" prepend=\",\" >                                                               "
                + "        <![CDATA[                                                                                                        "
                + "        template_name = #templateName#                                                                                   "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"title\" prepend=\",\" >                                                                      "
                + "        <![CDATA[                                                                                                        "
                + "        title = #title#                                                                                                  "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"remark\" prepend=\",\" >                                                                     "
                + "        <![CDATA[                                                                                                        "
                + "        remark = #remark#                                                                                                "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"fileName\" prepend=\",\" >                                                                   "
                + "        <![CDATA[                                                                                                        "
                + "        file_name = #fileName#                                                                                           "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"groupName\" prepend=\",\" >                                                                  "
                + "        <![CDATA[                                                                                                        "
                + "        group_name = #groupName#                                                                                         "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "      <isNotNull property=\"rank\" prepend=\",\" >                                                                       "
                + "        <![CDATA[                                                                                                        "
                + "        rank = #rank#                                                                                                    "
                + "        ]]>                                                                                                              "
                + "      </isNotNull>                                                                                                       "
                + "    </dynamic>                                                                                                           "
                + "     WHERE id = #id#                                                                                                     "
                + "  </update>                                                                                                              "
                + "                                                                                                                         "
                + "  <select id=\"templateInfo.findById\" parameterClass=\"java.lang.Long\" resultMap=\"templateInfoDOResultMap\" >         "
                + "     SELECT                                                                                                              "
                + "    <include refid=\"templateInfo_columns\" />                                                                           "
                + "    <![CDATA[                                                                                                            "
                + "     FROM ld_template_info WHERE id = #id#                                                                               "
                + "    ]]>                                                                                                                  "
                + "  </select>                                                                                                              "
                + "                                                                                                                         "
                + "  <select id=\"templateInfo.queryGroupNameDistinct\" resultClass=\"String\" >                                            "
                + "    <![CDATA[                                                                                                            "
                + "     SELECT distinct group_name                                                                                          "
                + "     FROM ld_template_info order by group_name asc                                                                       "
                + "    ]]>                                                                                                                  "
                + "  </select>                                                                                                              "
                + "                                                                                                                         "
                + "  <delete id=\"templateInfo.deleteById\" parameterClass=\"java.lang.Long\" >                                             "
                + "    <![CDATA[                                                                                                            "
                + "     DELETE FROM ld_template_info WHERE id = #id#                                                                        "
                + "    ]]>                                                                                                                  "
                + "  </delete>                                                                                                              "
                + "                                                                                                                         "
                + "  <select id=\"templateInfo.findList\" resultMap=\"templateInfoDOResultMap\" parameterClass=\"templateInfoQuery\" >      "
                + "     SELECT                                                                                                              "
                + "    <include refid=\"templateInfo_columns\" />                                                                           "
                + "     FROM ld_template_info                                                                                               "
                + "    <include refid=\"templateInfo_queryParams\" />                                                                       "
                + "  </select>                                                                                                              "
                + "                                                                                                                         "
                + "</sqlMap>                                                                                                                ";
        System.out.println(str);
        System.out.println("======================================");
        format(str);
    }

}
